package com.hzhc.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzhc.entity.VersionContent;
import com.hzhc.entity.VersionInfo;
import com.hzhc.enumBean.Modular;
import com.hzhc.enumBean.ResultUtilCodeEnum;
import com.hzhc.mapper.VersionContentMapper;
import com.hzhc.mapper.VersionInfoMapper;
import com.hzhc.service.VersionInfoService;
import com.hzhc.utils.ResultUtil;
import com.hzhc.vo.VersionInfoVo;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 更新日志 服务实现类
 * </p>
 *
 * @author 功不唐捐
 * @since 2024-04-07
 */
@Service
@RequiredArgsConstructor
public class VersionInfoServiceImpl extends ServiceImpl<VersionInfoMapper, VersionInfo> implements VersionInfoService {
    private final VersionContentMapper versionContentMapper;

    @Override
    public List<VersionInfoVo> getVersionInfoPage(Page<VersionInfoVo> page, VersionInfoVo vo) {
        IPage<VersionInfoVo> iPage = baseMapper.getVersionInfoPage(page, vo);
        return iPage.getRecords();
    }

    @Override
    public ResultUtil<T> addVersionInfo(VersionInfo vo) {
        LambdaQueryWrapper<VersionInfo> lambdaQuery = Wrappers.<VersionInfo>lambdaQuery()
                .eq(VersionInfo::getVersionNo, vo.getVersionNo())
                .eq(VersionInfo::getSysType, vo.getSysType())
                .eq(VersionInfo::getDisable, false);
        Integer count = baseMapper.selectCount(lambdaQuery);
        if (count > 0) {
            return ResultUtil.build(ResultUtilCodeEnum.VERSION_INFO_EXIST);
        }
        vo.setDisable(false).setCreateTime(LocalDateTime.now());
        boolean b = save(vo);
        return b ? ResultUtil.ok() : ResultUtil.fail();
    }

    @Override
    public ResultUtil<T> updateVersionInfo(VersionInfo vo) {
        LambdaQueryWrapper<VersionInfo> lambdaQuery = Wrappers.<VersionInfo>lambdaQuery()
                .eq(VersionInfo::getVersionNo, vo.getVersionNo())
                .eq(VersionInfo::getDisable, false)
                .eq(VersionInfo::getSysType, vo.getSysType())
                .ne(VersionInfo::getId, vo.getId());
        Integer count = baseMapper.selectCount(lambdaQuery);
        if (count > 0) {
            return ResultUtil.build(ResultUtilCodeEnum.VERSION_INFO_EXIST);
        }
        boolean b = updateById(vo);
        return b ? ResultUtil.ok() : ResultUtil.fail();
    }

    @Override
    public ResultUtil<T> deleteVersionInfo(Long id) {
        VersionInfo versionInfo = baseMapper.selectById(id);
        versionInfo.setDisable(true);
        boolean b = updateById(versionInfo);
        List<VersionContent> list = getContentListById(id);
        for (VersionContent content : list) {
            content.setDisable(true);
            versionContentMapper.updateById(content);
        }
        return b ? ResultUtil.ok() : ResultUtil.fail();
    }

    @Override
    public ResultUtil<List<VersionInfoVo>> getVersionInfo(VersionInfo vo) {
        List<VersionInfoVo> list = new ArrayList<>();
        List<VersionInfo> versionInfos = baseMapper.selectList(Wrappers.<VersionInfo>lambdaQuery()
                .eq(VersionInfo::getDisable, false)
                .eq(vo.getSysType() != null, VersionInfo::getSysType, vo.getSysType())
                .eq(vo.getIsApp() != null, VersionInfo::getIsApp, vo.getIsApp())
                .orderByDesc(VersionInfo::getCreateTime)
        );
        for (VersionInfo versionInfo : versionInfos) {
            VersionInfoVo versionInfoVo = new VersionInfoVo();
            BeanUtils.copyProperties(versionInfo, versionInfoVo);
            List<VersionContent> versionContents = getContentListById(versionInfo.getId());
            versionInfoVo.setContents(versionContents);
            list.add(versionInfoVo);
        }
        Page<VersionInfoVo> page = new Page<>(1, list.size());
        return ResultUtil.build(list,page.getTotal(),page.getPages(), ResultUtilCodeEnum.SUCCESS);
    }

    @Override
    public ResultUtil<JSONObject> judgeVersion(String versionNo, String padappAccessPath) {
        if(StrUtil.isBlank(versionNo)){
            return ResultUtil.build(ResultUtilCodeEnum.VERSION_NUMBER_CANNOT_BE_EMPTY);
        }
        List<VersionInfo> versionInfos = list(Wrappers.<VersionInfo>lambdaQuery()
                .eq(VersionInfo::getDisable,false)
                .eq(VersionInfo::getDeviceType, Modular.IPAD.getValue())
                .orderByDesc(VersionInfo::getCreateTime));
        if(versionInfos.isEmpty()){
            return ResultUtil.build(ResultUtilCodeEnum.VERSION_NUMBER_IS_NULL);
        }
        VersionInfo versionInfo = versionInfos.get(0);
        ResultUtil result = new ResultUtil();
        result.setCode(ResultUtilCodeEnum.SUCCESS.getCode());
        if(versionInfo.getVersionNo().equals(versionNo)){
            JSONObject object = new JSONObject();
            object.put("sameversion",true);
            object.put("version",versionInfo.getVersionNo());
            return ResultUtil.build(object,ResultUtilCodeEnum.SUCCESS.getCode(),"版本一致");
        }else {
            JSONObject object = new JSONObject();
            object.put("sameversion",false);
            String path = padappAccessPath.replaceFirst("/", "").replace("*", "");
            object.put("url",path+versionInfo.getVersionNo()+".apk");
            object.put("version",versionInfo.getVersionNo());
            return ResultUtil.build(object,ResultUtilCodeEnum.SUCCESS.getCode(),"版本不一致");
        }
    }

    private List<VersionContent> getContentListById(long id) {
        return versionContentMapper
                .selectList(Wrappers.<VersionContent>lambdaQuery().eq(VersionContent::getDisable, false)
                        .eq(VersionContent::getVersionInfoId, id).orderByDesc(VersionContent::getCreateTime));
    }
}
